package com.holystone.orca.itsm.system.service.dubbo;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.holystone.orca.itsm.system.api.SystemServiceApi;
import com.holystone.orca.itsm.system.model.*;
import com.holystone.orca.itsm.system.service.*;
import com.holystone.orca.itsm.system.service.common.SysConstant;
import com.holystone.orca.itsm.system.util.ListToMapUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service(
        version = "${itsm.system.service.version}",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}"
)
public class SystemServiceApiImpl implements SystemServiceApi {
    @Autowired
    private SysPrivilegeService privilegeService;
    @Autowired
    private SysRoleService roleService;
    @Autowired
    private SysModuleService moduleService;
    @Autowired
    private SysMenuService menuService;
    @Autowired
    private SysBtnService btnService;
    @Autowired
    private SysUserRoleService userRoleService;
    @Autowired
    private SysUserService userService;
    /**
     * 查询角色资源
     * @return Map《Url,角色》
     */
    @Override
    public Map<String, Set<String>> selectRolePermission() {
        List<SysPrivilege> privileges = privilegeService.selectActiveSysPrivilege();
        Map<String,SysRole> roleMap = ListToMapUtil.ListToMap(roleService.selectActiveSysRole(),"getRoleId");
        Map<String,SysModule> moduleMap = ListToMapUtil.ListToMap(moduleService.selectActiveSysModule(),"getModuleId");
        Map<String,SysBtn> btnMap = ListToMapUtil.ListToMap(btnService.selectActiveSysBtn(),"getMenuBtnId");
        Map<String,SysMenu> menuMap = ListToMapUtil.ListToMap(menuService.selectActiveSysMenu(),"getMenuId");

        Map<String, Set<String>> urlRoleMap = new HashMap<>();
        for(SysPrivilege privilege : privileges){

            if(privilege.getMasterType() == SysConstant.MASTER_TYPE_ROLE){
                String url = "";
                switch (privilege.getAccessType()){
                    case SysConstant.ACCESS_TYPE_MODULE:
                        url = moduleMap.get(privilege.getAccessId()+"").getModuleUrl();
                        break;
                    case SysConstant.ACCESS_TYPE_MENU:
                        url = menuMap.get(privilege.getAccessId()+"").getMenuUrl();
                        break;
                    case SysConstant.ACCESS_TYPE_BTN:
                        url = btnMap.get(privilege.getAccessId()+"").getMenuBtnUrl();
                }
                if(!urlRoleMap.containsKey(url)){
                    urlRoleMap.put(url,new HashSet<>());
                }
                SysRole role = roleMap.get(privilege.getMasterId()+"");
                urlRoleMap.get(url).add(role.getRoleCode());
            }
        }
        return urlRoleMap;
    }

    @Override
    public List<String> selectUserRole(Integer userId) {
        return userRoleService.selectUserRole(userId);
    }

    @Override
    public SysUser login(String username, String password) {
        return userService.login(username,password);
    }

    @Override
    public PageInfo<SysUser> pagingSysUser(SysUser user, Integer pageNO, Integer pageSize) {
        return userService.pagingSysUser(user, pageNO, pageSize);
    }

    @Override
    public SysUser addSysUser(SysUser sysUser) {
        return userService.addSysUser(sysUser);
    }

    @Override
    public JSONObject getUserMsg(Integer userId) {
        SysUser user = userService.selectByKey(userId);
        List<String> roles = selectUserRole(userId);
        JSONObject data = new JSONObject();
        data.put("roles",roles);
        data.put("name",user.getUserCode());
        return data;
    }
}
